//
//  PhotoViewCell.swift
//  MyWeiboApp
//
//  Created by 胡海波 on 15/7/6.
//  Copyright © 2015年 胡海波. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

protocol PhotoViewCellDelegate: NSObjectProtocol {
    
    func photoViewDidTapImage()
}
class PhotoViewCell: UICollectionViewCell,UIScrollViewDelegate {

    weak var photoViewDelegate: PhotoViewCellDelegate?
    
    var imageURL: NSURL? {

        didSet{
            
            resetScrollView()
            // 启动菊花
            indicator.startAnimating()
            
           imageView.sd_setImageWithURL(imageURL, placeholderImage: nil) { (image, error, _, _) in
            
                // 图片下载完成后 关闭菊花
                self.indicator.stopAnimating()

                // 计算缩放图片后适应屏幕的位置
                self.calPhotoWithImageToFitInScreen(image)
            
    
            }

        }
    }
    
    func resetScrollView(){
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
    }
    
    // 计算图片等比例显示屏幕的大小
    func calPhotoWithImageToFitInScreen(image:UIImage){
        
        let  ratio = image.size.height / image.size.width
        
        let h = bounds.width * ratio
        
        let size = CGSize(width: bounds.width, height: h)
        
        // 判断是否是长图 ，为长图则从图的最上方开始
        if bounds.height < size.height {
            
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentSize = size
        } else { // 短图 ，让图片显示在屏幕中央
            
            let y = (bounds.height - size.height) * 0.5
            
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            
            scrollView.contentInset = UIEdgeInsets(top: y, left: 0, bottom: 0, right: 0)
            
        }
    }
    
    
    // MARK --- UIScrollViewDelegate 
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        
        return imageView
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        // 重新计算边距
        var offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        if offsetX < 0 {
            offsetX = 0
        }
        
        var offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        if offsetY < 0 {
            offsetY = 0
        }
        
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
    }
    

    func clickImage(){
        
        photoViewDelegate?.photoViewDidTapImage()
        
    }

    override init(frame: CGRect) {

        super.init(frame: frame)

        scrollView.frame = bounds

        addSubview(scrollView)
        
        
        // 设置代理
        scrollView.delegate = self
        
        // 设置scrollView的最大最小缩放比例
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2.0
        
     
        scrollView.addSubview(imageView)
        // 添加手势
        let tap = UITapGestureRecognizer(target: self, action: "clickImage")
        imageView.addGestureRecognizer(tap)
        
        imageView.userInteractionEnabled = true
        
        // 添加指示器
        addSubview(indicator)
        indicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
        
    }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK -- 懒加载控件
    lazy private var scrollView = UIScrollView()
    lazy var imageView = UIImageView()
    lazy private var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
}
